package cn.edu.nuc.bre.controller;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import cn.edu.nuc.bre.dto.QueryHandler;
import cn.edu.nuc.bre.entity.Announcement;
import cn.edu.nuc.bre.service.interfaces.AnnouncementService;
import cn.edu.nuc.bre.util.PageInfo;

/**
 * 公告Action
 * 
 * @author Mine
 * @since 2016年11月10日_上午12:22:52
 */
@Controller
@Scope("prototype") // 原型
public class AnnouncementAction extends ActionSupport {
	/**
	 * 序列化id
	 */
	private static final long serialVersionUID = 1L;

	@Autowired
	private AnnouncementService announcementService;

	private String tip;// 回显小消息

	// 页面传入参数封装
	private Announcement anno = new Announcement();

	private List<Announcement> topAnnos = null;
	private PageInfo<Announcement> pageInfo = null;
	// 分页信息
	private int targetPage;
	// 查询key
	private String keyword;

	// 获取所有公告信息
	@Override
	public String execute() throws Exception {
		// 状态必须可见
		QueryHandler qu = QueryHandler.creat().andColumnMark("annostate", "!=", "0");
		if (targetPage > 0) {
			qu.limit(targetPage, 3);// 默认页面大小
		} else {
			qu.limitByPageSize(3);
		}
		if (keyword != null && !"".equals(keyword.trim())) {
			qu.andColumnLike("annocontent", keyword.trim());
		}
		qu.set();
		pageInfo = announcementService.findByQueryHandler(qu);
		// 获取滚动公告
		this.setTopAnnos(this.pullTopAnno());
		if (pageInfo == null) {
			return ERROR;
		} else {
			return SUCCESS;
		}
	}

	/**
	 * 删除公告
	 */
	public String delete() throws Exception {
		Integer id = this.getAnno().getAnnoid();
		if (announcementService.delete(id)) {
			this.setTip("删除[" + id + "]号公告成功!!!");
		} else {
			this.setTip("删除[" + id + "]号公告失败!!!");
		}
		return SUCCESS;
	}

	/**
	 * 获取发布公告页面
	 */
	public String showAdd() throws Exception {
		// 获取滚动公告
		this.setTopAnnos(this.pullTopAnno());
		return SUCCESS;
	}

	/**
	 * 发布公告
	 */
	public String add() throws Exception {
		if (announcementService.add(this.getAnno())) {
			// 添加成功
			this.setTip("添加公告成功!!!");
		} else {
			// 添加失败
			this.setTip("添加公告失败!!!");
		}
		return SUCCESS;
	}

	// 获取滚动公告
	private List<Announcement> pullTopAnno() {
		
		//计算出从当前时间开始算一周前的时间
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DAY_OF_MONTH, -7);
		String targetDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTimeInMillis());
		System.out.println(targetDate);
		
		// 状态必须可见
		QueryHandler qu = QueryHandler.creat().andColumnMark("annostate", "!=", "0");
		qu.OrderByASC("annotime").andColumnDateBetween("annotime", targetDate, null).set();
		PageInfo<Announcement> pi = announcementService.findByQueryHandler(qu);
		List<Announcement> list = pi.getBasePageList();
		System.out.println(list.size());
		return list;
	}

	public List<Announcement> getTopAnnos() {
		return topAnnos;
	}

	public void setTopAnnos(List<Announcement> topAnno) {
		this.topAnnos = topAnno;
	}

	public AnnouncementService getAnnouncementService() {
		return announcementService;
	}

	public void setAnnouncementService(AnnouncementService announcementService) {
		this.announcementService = announcementService;
	}

	public Announcement getAnno() {
		return anno;
	}

	public void setAnno(Announcement announcement) {
		this.anno = announcement;
	}

	public PageInfo<Announcement> getPageInfo() {
		return pageInfo;
	}

	public void setPageInfo(PageInfo<Announcement> annoPageInfo) {
		this.pageInfo = annoPageInfo;
	}

	public int getTargetPage() {
		return targetPage;
	}

	public void setTargetPage(int pageNumber) {
		this.targetPage = pageNumber;
	}

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String key) {
		this.keyword = key;
	}

	public String getTip() {
		return tip;
	}

	public void setTip(String tip) {
		this.tip = tip;
	}

}
